//
//  ViewController.m
//  set
//
//  Created by Xy on 2020/12/12.
//  Copyright © 2020 Xy. All rights reserved.
//

#import "ViewController.h"
#import "NotificationViewController.h"
#import "StartViewController.h"
#import "AppDelegate.h"
#import <AFNetworking.h>

@interface ViewController ()

@end

@implementation ViewController

- (instancetype)initWithUserName:(NSString *)userName Email:(NSString *)email UsedSize:(NSNumber *)usedSize MaxSize:(NSNumber *)maxSize
{
    self = [super init];
    if (self) {
        UIBarButtonItem *notificationButton = [[UIBarButtonItem alloc] init];
        [notificationButton setImage:[UIImage imageNamed:@"mespic"]];
        [notificationButton setTarget:self];
        [notificationButton setAction:@selector(notificationClicked)];
        self.navigationItem.rightBarButtonItem = notificationButton;
        [self.name setText:userName];
        [self.email setText:email];
        [self.size setText:[NSString stringWithFormat:@"%.1f / %.1f", ([usedSize doubleValue]/(1024*1024)), ([maxSize doubleValue]/(1024*1024))]];
        [self.surplus setText:[NSString stringWithFormat:@"%.1f", (([maxSize doubleValue]-[usedSize doubleValue])/(1024*1024))]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:self.icon];
    [self.view addSubview:self.name];
    [self.view addSubview:self.gender];
    [self.view addSubview:self.classl];
    [self.view addSubview:self.biol];
    [self.view addSubview:self.bio];
    [self.view addSubview:self.line];
    [self.view addSubview:self.emaill];
    [self.view addSubview:self.email];
    [self.view addSubview:self.sizell];
    [self.view addSubview:self.sizel];
    [self.view addSubview:self.size];
    [self.view addSubview:self.sizemb];
    [self.view addSubview:self.surplus];
    [self.view addSubview:self.surplusl];
    [self.view addSubview:self.surplusmb];
    [self.view addSubview:self.edit];
    [self.view addSubview:self.logout];
}

//头像
- (UIImageView*)icon{
    if (_icon == nil) {
        NSLog(@"icon");
        _icon = [[UIImageView alloc] init];
        _icon.frame = CGRectMake(137, 108, 140, 140);
        _icon.image = [UIImage imageNamed:@"touxiang"];
        _icon.layer.masksToBounds = YES;
        _icon.layer.cornerRadius = _icon.bounds.size.width * 0.5;
        _icon.layer.borderWidth = 1.5f;
        _icon.layer.borderColor = [UIColor whiteColor].CGColor;
        
    }
    return _icon;
}

//昵称
- (UILabel*)name{
    if (_name == nil) {
        _name = [[UILabel alloc] init];
        _name.frame = CGRectMake(137, 276, 140, 30);
        _name.font = [UIFont systemFontOfSize:20];
        _name.textAlignment = NSTextAlignmentCenter;
        _name.adjustsFontSizeToFitWidth = YES;
        _name.text = @"chenhhhhhh";
    }
    return _name;
}

//性别
-(UIImageView*)gender{
    if (_gender == nil) {
        _gender = [[UIImageView alloc] init];
        _gender.frame = CGRectMake(137, 314, 30, 30);
        _gender.image = [UIImage imageNamed:@"M.png"];
    }
    return _gender;
}

//用户类型
- (UILabel*)classl{
    if (_classl == nil) {
        _classl = [[UILabel alloc] init];
        _classl.frame = CGRectMake(175, 314, 102, 30);
        _classl.font = [UIFont systemFontOfSize:20];
        _classl.textAlignment = NSTextAlignmentRight;
        _classl.adjustsFontSizeToFitWidth = YES;
        _classl.text = @"用户类型：1";
    }
    return _classl;
}

//个人简介
- (UILabel*)biol {
    if (_biol == nil) {
        _biol = [[UILabel alloc] init];
        _biol.frame = CGRectMake(74, 373, 77, 30);
        _biol.font = [UIFont systemFontOfSize:20];
        _biol.textAlignment = NSTextAlignmentLeft;
        _biol.adjustsFontSizeToFitWidth = YES;
        _biol.text = @"个人简介";
    }
    return _biol;
}

- (UILabel*)bio{
    if (_bio == nil) {
        _bio = [[UILabel alloc] init];
        _bio.frame = CGRectMake(74, 368, 266, 75);
        _bio.font = [UIFont systemFontOfSize:20];
        _bio.textAlignment = NSTextAlignmentLeft;
        _bio.numberOfLines = 3;
        _bio.adjustsFontSizeToFitWidth = YES;
        _bio.lineBreakMode = NSLineBreakByTruncatingTail;
        _bio.backgroundColor = [UIColor colorWithRed:245 green:255 blue:250 alpha:1];
        _bio.text = @"这个人很懒，什么都没有写";
        
    }
    return _bio;
}

//分割线
- (UILabel*)line{
    if (_line == nil) {
        _line = [[UILabel alloc] init];
        _line.frame = CGRectMake(20, 500, 374, 1);
        _line.text = @"";
        _line.backgroundColor = UIColor.lightGrayColor;
    }
    return _line;
}

//邮箱
- (UILabel*)emaill{
    if (_emaill == nil) {
        _emaill = [[UILabel alloc] init];
        _emaill.frame = CGRectMake(61, 533, 55, 30);
        _emaill.text = @"邮箱";
        _emaill.textAlignment = NSTextAlignmentLeft;
    }
    return _emaill;
}

- (UILabel*)email{
    if (_email == nil) {
        _email = [[UILabel alloc] init];
        _email.frame = CGRectMake(61, 571, 292, 30);
        _email.text = @"chenhh53@mail2.sysu.edu.cn";
        _email.textAlignment = NSTextAlignmentLeft;
        _email.adjustsFontSizeToFitWidth = YES;
    }
    return _email;
}

//存储额度
- (UILabel*)s{
    if (_sizel == nil) {
        _sizel = [[UILabel alloc] init];
        _sizel.frame = CGRectMake(61, 600, 80, 30);
        _sizel.text = @"存储用量";
        _sizel.textAlignment = NSTextAlignmentLeft;
    }
    return _sizel;
}

- (UILabel*)sizell{
    if (_sizell == nil) {
        _sizell = [[UILabel alloc] init];
        _sizell.frame = CGRectMake(61, 625, 80, 30);
        _sizell.text = @"详细：";
        _sizell.textAlignment = NSTextAlignmentLeft;
    }
    return _sizell;
}

- (UILabel*)size{
    if (_size == nil) {
        int max = 8388608;
        double maxmb = max / 1048576;
        int used = 1021582;
        double usedmb = used / 1048576;
        _size = [[UILabel alloc] init];
        _size.frame = CGRectMake(165, 625, 131, 30);
        _size.textAlignment = NSTextAlignmentRight;
        //_size.text = @"%f // %f", usedmb, maxmb;
        _size.text = @"0.97 / 8.0";
    }
    return _size;
}

- (UILabel*)sizemb{
    if (_sizemb == nil) {
        _sizemb = [[UILabel alloc] init];
        _sizemb.frame = CGRectMake(311, 625, 42, 30);
        _sizemb.text = @"MB";
        _sizemb.textAlignment = NSTextAlignmentLeft;
    }
    return _sizemb;
}

- (UILabel*)surplusl{
    if (_surplusl == nil) {
        _surplusl = [[UILabel alloc] init];
        _surplusl.frame = CGRectMake(61, 655, 80, 30);
        _surplusl.text = @"剩余：";
        _surplusl.textAlignment = NSTextAlignmentLeft;
    }
    return _surplusl;
}

- (UILabel*)surplus{
    if (_surplus == nil) {
        int max = 8388608;
        int used = 1021582;
        double surplusmb = (max - used) / 1048576;
        
        _surplus = [[UILabel alloc] init];
        _surplus.frame = CGRectMake(165, 655, 131, 30);
        //_surplus.text = @"%f", surplusmb;
        _surplus.text = @"7.03";
        _surplus.textAlignment = NSTextAlignmentRight;
    }
    return _surplus;
}

- (UILabel*)surplusmb{
    if (_surplusmb == nil) {
        _surplusmb = [[UILabel alloc] init];
        _surplusmb.frame = CGRectMake(311, 655, 42, 30);
        _surplusmb.text = @"MB";
        _surplusmb.textAlignment = NSTextAlignmentLeft;
    }
    return _surplusmb;
}

//底部按钮
//编辑资料
- (UIButton*)edit{
    if (_edit == nil) {
        _edit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_edit setFrame:CGRectMake(20, 700, 187, 59)];
        [_edit setTitle:@"编辑" forState:UIControlStateNormal];
        [_edit.layer setMasksToBounds:YES];
        [_edit.layer setCornerRadius:5.0];
        _edit.backgroundColor = [UIColor colorWithRed:240 green:248 blue:255 alpha:1];
        _edit.layer.borderColor = [UIColor colorWithRed:224 green:238 blue:238 alpha:1].CGColor;
        _edit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _edit.titleLabel.font=[UIFont systemFontOfSize:35];
    }
    return _edit;
}

//退出登陆
-(UIButton*)logout{
    if (_logout == nil) {
        _logout = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_logout addTarget:self action:@selector(logoutClicked) forControlEvents:UIControlEventTouchUpInside];
        [_logout setFrame:CGRectMake(210, 700, 187, 59)];
        [_logout setTitle:@"登出" forState:UIControlStateNormal];
        [_logout.layer setMasksToBounds:YES];
        [_logout.layer setCornerRadius:5.0];
        _logout.backgroundColor = [UIColor colorWithRed:240 green:248 blue:255 alpha:1];
        _logout.layer.borderColor = [UIColor colorWithRed:224 green:238 blue:238 alpha:1].CGColor;
        _logout.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        _logout.titleLabel.font=[UIFont systemFontOfSize:35];
    }
    return _logout;
}

- (void)notificationClicked
{
    NotificationViewController *VC = [[NotificationViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

-(void)logoutClicked
{
    AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/user/logout"];
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    [manager POST:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        StartViewController *VC = [[StartViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
        [myDelegate.window setRootViewController:nav];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"failed");
    }];
}

@end
